package com.dempe.forest.common.utils;

import java.math.BigInteger;
import java.nio.charset.Charset;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

/**
 * Created with IntelliJ IDEA.
 * User: Dempe
 * Date: 2016/4/8
 * Time: 15:48
 * To change this template use File | Settings | File Templates.
 */
public class Utils {
    public static String md5Str(String value) throws NoSuchAlgorithmException {
        MessageDigest m = MessageDigest.getInstance("MD5");
        m.reset();
        m.update(value.getBytes());
        byte[] digest = m.digest();
        BigInteger bigInt = new BigInteger(1,digest);
        String hashtext = bigInt.toString(16);
// Now we need to zero pad it if you actually want the full 32 chars.
        while(hashtext.length() < 32 ){
            hashtext = "0"+hashtext;
        }
        return hashtext;
//        byte[] md5s = MessageDigest.getInstance("MD5").digest(value.getBytes(Charset.defaultCharset()));
//        return new String(md5s);
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String s = Utils.md5Str("localhost|" + 9999 + "|forest-admin");
        System.out.println(s);
    }
}
